<?php
//模拟闹钟功能,需要1.mp3作为响铃音频文件
header("content-type:text/html;charset=utf-8");
session_start();
if($_POST){
	if($_POST['time'] !=''){
		$tt=$_POST['time'];
		$tt=explode(".", $tt);
		if(count($tt)==1){
			$tts=$tt[0];
		}elseif(count($tt)==2){
			$tts=$tt[0]*60+$tt[1];
		}elseif(count($tt)==3){
			$tts=$tt[0]*3600+$tt[1]*60+$tt[2];
		}else{
			echo "<script>";
			echo "alert('时间格式不正确!');";
			echo "window.location.href='naoz-0.php';";
			echo "</script>";
			exit;
		}
		//需要时间输入
		$cut=time();
		$pname=$_POST['name'] ? $_POST['name'] : "默认项";
		$_SESSION["t".$cut]['name']=$pname;
		$_SESSION["t".$cut]['time']=$tts + time();
	}else{
		echo "<script>";
		echo "alert('必须输入时间!');";
		echo "window.location.href='naoz-0.php';";
		echo "</script>";
	}
}
if($_GET['deltd']){
	$td=$_GET['deltd'];
	unset($_SESSION[$td]);
}
if($_GET['delall']){
	session_destroy();
	echo "<script>";
	echo "alert('所有定时都被清除！');";
	echo "window.location.href='naoz-0.php';";
	echo "</script>";
}
$arrt=$_SESSION;
foreach($_SESSION as $k=>$v){
	$arras[$k]=$v['time'];
}
asort($arras);
asort($arrt);
echo "<pre>";
//print_r($_SESSION);
//print_r($arras);
echo "</pre>";
?>
<html>
	<head>
		<title>noaz-0</title>
	</head>
	<body>
		<table border="1" cellspacing="0" cellpadding="5">
			<tr>
				<th>排序</th>
				<th>倒计时项</th>
				<th>剩余时间</th>
				<th>终止节点</th>
				<th>操作</th>
			</tr>
<?php
foreach($arras as $k=>$v){
	$i++;
?>
			<tr>
				<td><?php echo $i;?></td>
				<td id="tdn-<?php echo $i?>"><?php echo $arrt[$k]['name']?></td>
				<td id="td-<?php echo $i?>"></td>
				<td id="tdh-<?php echo $i?>" class="tdh" ><?php echo $v;?></td>
				<td><a href="naoz-0.php?deltd=<?php echo $k?>">删除</a></td>
			</tr>
<?php
}
?>
		</table>
		<audio id="mzc" loop="loop" controls="controls" src="1.mp3">不支持</audio><a href="naoz-0.php?delall=all">清除所有定时</a>
		<form action="#" method="post">
			<input type="text" name="name" id="" value="" />
			<input type="text" name="time" id="" value="" />eq: h.i.s | i.s<br />
			<input type="submit" value="添加定时"/>
		</form>
	</body>
<script type="text/javascript">
	function tdjj(i){
		var timestamp=new Date().getTime();
		var ts=timestamp/1000;
		var tz=Math.floor(ts);
		var tdid=document.getElementById("td-"+i);
		var tdhid=document.getElementById("tdh-"+i);
		var tdhval=tdhid.innerHTML;
		var tdend=tdhval-tz;
		if(tdend>0){
			var tdm=Math.floor(tdend/60);
			if(tdm<120){
				var tds=tdend%60;
				tdid.innerHTML=tdm+" 分 "+tds+" 秒 ";
			}else{
				var tdh=Math.floor(tdm/60);
				var tdmm=tdm%60;
				var tds=tdend%60;
				tdid.innerHTML=tdh+" 时"+tdmm+" 分 "+tds+" 秒 ";
			}
		}
		if(tdend<0){
			tdend=Math.abs(tdend);
			var tdm=Math.floor(tdend/60);
			var tds=tdend%60;
			tdid.innerHTML="过时 : "+tdm+" 分 "+tds+" 秒 ";
		}
		if(tdend==0){
			var mp3=document.getElementById("mzc");
			mp3.play();
			var tdn=document.getElementById("tdn-"+i);
			var tdname=tdn.innerHTML;
			alert(tdname);
		}
	}
	var iend=<?php echo count($arrt)+1;?>;
	for(var i=1;i<iend;i++){
		setInterval("tdjj("+i+")",1000);
	}
</script>
<script type="text/javascript" src="/public/js/footer.js"></script>
</html>